package cate.game.pay;

import cate.common.table.d.GDObj;
import cate.common.table.d.GDOperation;
import cate.common.table.d.GDProp;
import cate.game.framework.GameException;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cp.solution.business.pay.PayNote;
import cp.solution.business.pay.PayRecordEntity;
import cp.solution.business.pay.dto.PayDeliverResult;
import easy.java.codec.JsonCodec;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class OutsidePayHandler {

    public static synchronized PayDeliverResult exe(Role role, PayRecordEntity record, PayDeliverParam param) {
        PayDeliverResult r = new PayDeliverResult();
        r.setGsVer("第1版外部支付处理逻辑");
        PayNote note;
        try {
            note = JsonCodec.instance().decode(record.getNote(), PayNote.class);
        } catch (Exception e) {
            log.error("", e);
            throw new GameException().content("解析payNote失败, payNote="+record.getNote());
        }
        long vipPoints;
        if(StringUtils.isBlank(note.yzxProductId)) {
            // 代币发放
            int tokenMoney = note.yzxWareCount + note.yzxActivityExtra;
            vipPoints = note.yzxWareCount * 10;
            log.info("叶子戏外部支付~发放代币数量={}+{}={}, vip积分={}, 订单uid={}", note.yzxWareCount, note.yzxActivityExtra,
                    tokenMoney, vipPoints, record.getUid());
            new MixResAdder()
                    .setRes(GDObj.getMixResStr(GDObj.Type.PROP, GDProp.TID.TOKEN_MONEY, tokenMoney))
                    .setOperation(GDOperation.PAY_OUTSIDE)
                    .setOperationSub(tokenMoney)
                    .setDialogReward(true)
                    .exe(role);
        } else {
            // 道具发放
            vipPoints = record.payCents/10;
            log.info("叶子戏外部支付~发放道具={}, vip积分={}, 订单uid={}", note.yzxProductId, vipPoints, record.getUid());
            new MixResAdder()
                    .setRes(note.yzxProductId)
                    .setOperation(GDOperation.PAY_OUTSIDE)
                    .setOperationSub(0)
                    .setDialogReward(true)
                    .exe(role);
        }

        role.getPay().afterPay(record, vipPoints, param);
        return r;
    }
}
